package com.site.servlet;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import com.wu.util.HttpUtil;

@WebServlet(name = "IndexServlet", urlPatterns = "/indexServlet")
public class IndexServlet extends HttpServlet {
	private static Logger logger = Logger.getLogger(IndexServlet.class);

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(req, response);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {
		Date begin=new Date();
		if ("1".equals(req.getParameter("getCode"))) {
			List code=new ArrayList();
			try {
				code = HttpUtil.getCookie("https://api.liqiang365.com/validcode/get");
			} catch (Exception e) {
				e.printStackTrace();
			}
			response.setCharacterEncoding("UTF-8");
			
			Cookie cook=new Cookie("cookie_brsy_pic_code",code.get(2).toString().substring(code.get(2).toString().lastIndexOf("=")+1));
			//System.out.println(code.get(0).toString());
			//System.out.println(code.get(2).toString().substring(code.get(2).toString().lastIndexOf("=")+1));
			
			response.addCookie(cook);
			ServletOutputStream op=response.getOutputStream();
			op.write((byte[]) code.get(0));
			op.flush();
			op.close();
		} else {
			String actionName = req.getParameter("actionName");
			String url =  actionName;
			Map pa = req.getParameterMap();
			Iterator<String> keys = pa.keySet().iterator();
			while (keys.hasNext()) {
				String key = (String) keys.next();
				String[] value = (String[]) pa.get(key);
				if (!key.equals("actionName")) {
					if (url.contains("?")) {
						url = url + "&" + key + "=" + value[0];
					} else {
						url = url + "?" + key + "=" + value[0];
					}
				}
			}
			String webUrl = req.getSession().getServletContext().getRealPath("");
			String body = HttpUtil.sendPost(url);
			String images[] = body.split(".jpg");
			for (String tmp : images) {
				int index = tmp.lastIndexOf("http://");
				if (index > 0) {
					String imageUrl = tmp.substring(index) + ".jpg";
					String imgaName = imageUrl.substring(imageUrl.lastIndexOf("/"));
					try {
						HttpUtil.download(imageUrl, imgaName, webUrl + File.separator+"images"+File.separator);
						body = body.replaceAll(imageUrl.substring(0, imageUrl.lastIndexOf("/")), "images");
					} catch (Exception e) {
						System.out.println(e.toString());
					}
				}
			}
			System.out.println("耗时："+(new Date().getTime()-begin.getTime()));
			response.setCharacterEncoding("UTF-8");
			response.getWriter().append(body);
		}
	}
}
